package com.bigdata

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

object Demo24Jdbc {

  def main(args: Array[String]): Unit = {

    Class.forName("com.mysql.jdbc.Driver")
    var conn: Connection = null
    try {
      // 获取连接
      conn = DriverManager.getConnection("jdbc:mysql://localhost/news?useUnicode=true&characterEncoding=UTF-8",
        "root", "root")

      // val ps: PreparedStatement = conn.prepareStatement("select * from emp where empno = ?")
      val ps: PreparedStatement = conn.prepareStatement("select * from emp where ename = ?")

      // ps.setString(1, "7788")
      ps.setString(1, "春哥")

      val resultSet: ResultSet = ps.executeQuery()

      while (resultSet.next()) {
        val empno: String = resultSet.getString(1)
        val ename: String = resultSet.getString("ename")
        val job: String = resultSet.getString("job")
        val sal: String = resultSet.getString("sal")
        println(s"$empno---$ename---$job---$sal")
      }
    } catch {
      case e: Exception =>
        e.printStackTrace()
    } finally {
      conn.close()
    }


  }

}
